import csv
from selenium import webdriver#用于操作浏览器
from selenium.webdriver.chrome.options import Options#用于设置谷歌浏览器
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
#创建设置浏览器对象
def set():
    q1=Options()
    #禁用浏览器的沙盒模式(增加系统兼容性)
    q1.add_argument('--no-sandbox')
    #保持浏览器打开状态（默认是代码执行完毕自动关闭）
    q1.add_experimental_option('detach', True)
    #一般情况下上述三行代码是不需要动的

    #创建并启动浏览器
    a1=webdriver.Chrome(service=Service('C:\Program Files\Google\Chrome\Application\chromedriver.exe'),options=q1)#传驱动器的路径
    return a1
a1=set()
a1.get('https://www.shanghairanking.cn/rankings/bcur/2024')

all_school_info=[]
for page in range(1,21):
    schools = a1.find_elements(By.XPATH, '//*[@id="content-box"]/div[2]/table/tbody//tr')

    for school in schools:
        try:
            rank =school.find_element(By.XPATH,'./td[1]/div').text
            name = school.find_element(By.XPATH, './td[2]/div/div[2]/div[1]/div/div/span').text
            english_name = school.find_element(By.XPATH, './td[2]/div/div[2]/div[2]/div/div/span').text
            region = school.find_element(By.XPATH, './td[3]').text
            rating = school.find_element(By.XPATH, './td[5]').text
            school_running_level=school.find_element(By.XPATH,'./td[6]').text
            print(f"排名: {rank}")
            print(f"学校名称: {name}")
            print(f"英文名称: {english_name}")
            print(f"地区: {region}")
            print(f"评分: {rating}")
            print(f"办学层次: {school_running_level}")
            school_info = {
                '排名': rank,
                '学校名称': name,
                '英文名称': english_name,
                '地区': region,
                '评分': rating,
                '办学层次': school_running_level
            }

            all_school_info.append(school_info)
            #print(all_school)
        except Exception as e:
            print('无法获取该数据')
            continue
    if page<4 or page==19 or page==18:
        a1.find_element(By.XPATH,'/html/body/div/div/div/div[2]/div/div[2]/div[2]/div[1]/div/ul/li[9]/a').click()
    elif page<5 or page==17 :
        xpath = '//*[@id="content-box"]/ul/li[10]/a'
        a1.find_element(By.XPATH,xpath).click()
    elif page<17:
        a1.find_element(By.XPATH,'//*[@id="content-box"]/ul/li[11]/a').click()




def save_to_csv(all_university_info, filename='大学排名信息.csv'):
    with open(filename, 'w', newline='', encoding='utf-8-sig') as csvfile:
        header = ['排名', '学校名称', '英文名称', '地区', '评分', '办学层次']
        writer = csv.DictWriter(csvfile,fieldnames=header)
        writer.writeheader()

        writer.writerows(all_university_info)
        csvfile.close()

save_to_csv(all_school_info)
